package com.iad.controller.command
{
  import com.adobe.cairngorm.commands.Command;
  import com.adobe.cairngorm.control.CairngormEvent;
  import com.adobe.cairngorm.control.CairngormEventDispatcher;
  import com.iad.controller.event.*;
  import com.iad.controller.manager.*;
  import com.iad.controller.service.*;
  import com.iad.model.*;
  import com.iad.model.valueobject.*;
  import com.iad.view.component.*;
  import com.iad.view.form.*;
  import com.iad.view.util.ascb.util.*;
  
  import mx.collections.ArrayCollection;
  import mx.managers.CursorManager;
  import mx.rpc.events.FaultEvent;
  
  public class SiteCommand implements Command
  {
    [Bindable]
    private var model:ModelLocatorImpl = ModelLocatorImpl.getInstance();
    
    private var form:ProfileEdit;
 
    public function execute(event:CairngormEvent):void {
      switch (event.type) {
        case SiteEvent.NEW_SITE:
          newSite();
        break;
        case SiteEvent.GET_SITE:
          getSite();
        break;
        case SiteEvent.UPDATE_SITE_PROFILE:
          updateSiteProfile(event as SiteEvent);
        break;
        case SiteEvent.GET_SITE_BY_LABEL:
          getSiteByLabel();
        break;
        case SiteEvent.GET_SITES:
          getSites();
        break;
        case SiteEvent.GET_SITE_VIDEOS:
          getSiteVideos();
        break;
        case SiteEvent.SUBMIT_REVIEW:
          submitReview(event as SiteEvent);
        break;
        case SiteEvent.MODERATE_REVIEWS:
          moderateReviews();
        break;
        case SiteEvent.APPROVE_REVIEWS:
          approveReviews();
        break;
     }
    }
    
    
    private function getReviews():void {
      var service:SiteService = new SiteService();
      service.addEventListener(ServiceResultEvent.GET_REVIEWS_RESULT, getReviewsOnResult, false, 0, false);
      service.addEventListener(ServiceErrorEvent.ERROR, onError, false, 0, true);
      service.getReviews(model.currentSiteId);
    }
    
    
    private function moderateReviews():void {
      var service:SiteService = new SiteService();
      service.addEventListener(ServiceResultEvent.GET_NEW_REVIEWS_RESULT, getNewReviewsOnResult, false, 0, false);
      service.addEventListener(ServiceErrorEvent.ERROR, onError, false, 0, true);
      service.getNewReviews(model.currentSiteId);
    }
    
    
    protected function getReviewsOnResult(event:ServiceResultEvent):void {
      trace("SiteCommand::getReviewsOnResult()");
      var reviews:ArrayCollection = event.result as ArrayCollection;
      model.currentSite.reviews = reviews;
    }
    
    protected function getNewReviewsOnResult(event:ServiceResultEvent):void {
      trace("SiteCommand::getNewReviewsOnResult()");
      var reviews:ArrayCollection = event.result as ArrayCollection;
      
      for each(var review:Review in reviews) {
        // initially set all new reviews to approved
        review.approved = true;
        trace(review);
      }
      model.currentSite.reviews = reviews;
      model.app.adminToolPanel.reviewsButton.label = "Approve Reviews";
      model.approveReviewsMode = true;
    }
    
    private function approveReviews():void {
      var service:SiteService = new SiteService();
      service.addEventListener(ServiceResultEvent.APPROVE_NEW_REVIEWS_RESULT, approveNewReviewsOnResult, false, 0, false);
      service.addEventListener(ServiceErrorEvent.ERROR, onError, false, 0, true);
      service.approveNewReviews(model.currentSiteId);
    }
    
    
    protected function approveNewReviewsOnResult(event:ServiceResultEvent):void {
      trace("SiteCommand::approveNewReviewsOnResult()");
      var reviews:ArrayCollection = event.result as ArrayCollection;
      
      for each(var review:Review in reviews) {
        trace(review);
      }
      model.currentSite.reviews = reviews;
      model.app.adminToolPanel.reviewsButton.label = "Moderate Reviews";
      model.approveReviewsMode = false;
    }
    
    
    private function submitReview(event:SiteEvent):void {
      var service:SiteService = new SiteService();
      service.addEventListener(ServiceResultEvent.SUBMIT_REVIEW_RESULT, submitReviewOnResult, false, 0, false);
      service.addEventListener(ServiceErrorEvent.ERROR, onError, false, 0, true);
      service.submitReview(event.object as Review);
    }
    
    private function updateSiteProfile(event:SiteEvent):void {
      var service:SiteService = new SiteService();
      service.addEventListener(ServiceResultEvent.SUBMIT_REVIEW_RESULT, updateSiteProfileOnResult, false, 0, false);
      service.addEventListener(ServiceErrorEvent.ERROR, onError, false, 0, true);
      form = event.object as ProfileEdit;
      var obj:Object = new Object();
      obj.bandName = form.bandName.text;
      obj.pathLabel = form.siteUrl.text;
      obj.bandCity = form.bandCity.text;
      obj.bandState = form.bandState.text;
      obj.firstName = form.firstName.text;
      obj.lastName = form.lastName.text;
      obj.email = form.email.text;
      obj.password = form.password.text;
      obj.url = form.externalUrl.text;
      service.updateSiteProfile(model.currentSiteId, obj);
    }
    
    
    
    private function getSite():void {
      var service:SiteService = new SiteService();
      service.addEventListener(ServiceResultEvent.GET_SITE_RESULT, getSiteOnResult, false, 0, false);
      service.addEventListener(ServiceErrorEvent.ERROR, onError, false, 0, true);
      service.getSite(model.currentSiteId);
    }
    
    private function getSiteByLabel():void {
      var service:SiteService = new SiteService();
      service.addEventListener(ServiceResultEvent.GET_SITE_BY_LABEL_RESULT, getSiteOnResult, false, 0, false);
      service.addEventListener(ServiceErrorEvent.ERROR, onError, false, 0, true);
      service.getSiteByLabel(model.currentSiteLabel);
    }
    
        
    private function getSites():void {
      var service:SiteService = new SiteService();
      service.addEventListener(ServiceResultEvent.GET_SITES_RESULT, getSitesOnResult, false, 0, false);
      service.addEventListener(ServiceErrorEvent.ERROR, onError, false, 0, true);
      service.getSites();
    }
    
    
    protected function submitReviewOnResult(event:ServiceResultEvent):void {
      getReviews();
    }
    
    protected function updateSiteProfileOnResult(event:ServiceResultEvent):void {
      trace("SiteCommand::updateSiteProfileOnResult()");
      //form.setSuccessMessage();
      CairngormEventDispatcher.getInstance().dispatchEvent(new SiteEvent(SiteEvent.GET_SITE, model.currentSiteId));
    }
    
    
    protected function getSitesOnResult(event:ServiceResultEvent):void {
      trace("SiteCommand::getSitesOnResult()");
      var sites:ArrayCollection = event.result as ArrayCollection;
      model.sites = sites;
    }
    
    
    private function getSiteVideos():void {
      var service:SiteService = new SiteService();
      service.addEventListener(ServiceResultEvent.GET_SITE_VIDEOS_RESULT, getSiteVideosOnResult, false, 0, false);
      service.addEventListener(ServiceErrorEvent.ERROR, onError, false, 0, true);
      service.getSiteVideos(model.currentSiteId);
    }
    
    
    protected function getSiteVideosOnResult(event:ServiceResultEvent):void {
      trace("SiteCommand::getSiteVideosOnResult()");
      var videos:ArrayCollection = event.result as ArrayCollection;
      model.videos = videos;
    }
    
        
    protected function getSiteOnResult(event:ServiceResultEvent):void {
      trace("SiteCommand::getSiteOnResult()");
      
      var site:Site = event.result as Site;
      var tmpArray:Array = site.playlists.toArray().concat(site.videos.toArray());
      model.discographyItems =  new ArrayCollection(tmpArray);
      model.currentSite = site;
      model.playlistSiteId = site.id;
      model.currentSiteId = site.id;
      DeepLinkManager.getInstance().updateURL(site.pathLabel, site.name);
      
      trace(model.currentSite.toString());
      model.app.currentState = model.ARTIST_VIEW;
      CairngormEventDispatcher.getInstance().dispatchEvent(new PageEvent(PageEvent.GET_PAGES, model.currentSite.id));
    }
    
    protected function getSiteOnFault(event:FaultEvent):void {
      trace("ERROR fetching site:\n" + event.message.toString());
    }
    
    private function newSite():void {
      var service:SiteService = new SiteService();
      service.addEventListener(ServiceResultEvent.NEW_SITE_RESULT, newSiteOnResult, false, 0, false);
      service.addEventListener(ServiceErrorEvent.ERROR, onError, false, 0, true);
      service.newSite(model.currentSite);
    }
    
     
    protected function newSiteOnResult(event:ServiceResultEvent):void {
      trace("SiteCommand::newSiteOnResult()");
      
      var site:Site = event.result as Site;
      var tmpArray:Array = site.playlists.toArray().concat(site.videos.toArray());
      model.discographyItems =  new ArrayCollection(tmpArray);
      model.currentSite = site;
      model.playlistSiteId = site.id;
      model.currentSiteId = site.id;
      model.signupWizard.selectedChild = model.signupWizard.step2;
      model.signupWizard.step1.enabled = false;
      model.signupWizard.step2.enabled = true;
      CairngormEventDispatcher.getInstance().dispatchEvent(new PageEvent(PageEvent.GET_PAGES, model.currentSiteId));
      //model.currentSong = null;
      model.songsArray = null;
      CairngormEventDispatcher.getInstance().dispatchEvent(new PlaylistEvent(PlaylistEvent.GET_PLAYLIST_FROM_NEW_SITE, model.currentSiteId));
      
      var welcomeEmail:WelcomeEmail = new WelcomeEmail();
      welcomeEmail.subject = "Welcome to Independent Artists Direct"; 
      welcomeEmail.author =  "\"Independent Artists Direct\" <support@independentartistsdirect.com>"
      welcomeEmail.recipient =  site.email;
      welcomeEmail.bandName =  site.name;
      welcomeEmail.firstName =  site.firstName;
      welcomeEmail.lastName =  site.lastName;
      welcomeEmail.login =  site.pathLabel;
      welcomeEmail.password =  site.password;
      CairngormEventDispatcher.getInstance().dispatchEvent(new SignupEvent(SignupEvent.SEND_WELCOME_EMAIL, welcomeEmail));
    }
    
     
    private function onError(event:ServiceResultEvent):void {
      CursorManager.removeBusyCursor();
      trace("error: " + event);
    }
     
  } 
}
    